﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Input;

namespace PonGun
{
    public enum PickupType : int
    {
        Ammo = 0,
        CityRepair,
        CityReserection,
        Gun,
        Heal,
        Nova,
        Shield,
        MultiBall,
        MinusBall,
        FlipH,
        FlipV,
        ScreenSpin,
        Mist,
        LevelUp,
        LastRecord,
    }

    public class Pickup : Base2DObject
    {
        public ObjectCallBack OnPickupDied;
        PickupType _PickupTyp { get; set; }
        public PickupType PickupTyp
        {
            get { return _PickupTyp; }
            set
            {
                _PickupTyp = value;
                switch (_PickupTyp)
                {
                    case PickupType.Ammo:
                        Asset = "Textures/Pickups/Ammo";
                        Color = Color.SteelBlue;
                        break;
                    case PickupType.CityRepair:
                        Asset = "Textures/Pickups/CityRepair";
                        Color = Color.Gold;
                        break;
                    case PickupType.CityReserection:
                        Asset = "Textures/Pickups/CityReserection";
                        Color = Color.White;
                        break;
                    case PickupType.Gun:
                        Asset = "Textures/Pickups/Gun";
                        Color = Color.SlateGray;
                        break;
                    case PickupType.Heal:
                        Asset = "Textures/Pickups/Heal";
                        Color = Color.LimeGreen;
                        break;
                    case PickupType.Nova:
                        Asset = "Textures/Pickups/Nova";
                        Color = Color.Azure;
                        break;
                    case PickupType.Shield:
                        Asset = "Textures/Pickups/Shield";
                        Color = Color.BlueViolet;
                        break;
                    case PickupType.MultiBall:
                        Asset = "Textures/Pickups/MultiBall";
                        Color = Color.CadetBlue;
                        break;
                    case PickupType.MinusBall:
                        Asset = "Textures/Pickups/MinusBall";
                        Color = Color.Crimson;
                        break;
                    case PickupType.FlipH:
                        Asset = "Textures/Pickups/FlipH";
                        Color = Color.Violet;
                        break;
                    case PickupType.FlipV:
                        Asset = "Textures/Pickups/FlipV";
                        Color = Color.Violet;
                        break;
                    case PickupType.ScreenSpin:
                        Asset = "Textures/Pickups/ScreenSpin";
                        Color = Color.OrangeRed;
                        break;
                    case PickupType.Mist:
                        Asset = "Textures/Pickups/Mist";
                        Color = Color.Snow;
                        break;
                    case PickupType.LevelUp:
                        Asset = "Textures/Pickups/LevelUp";
                        Color = Color.LightGoldenrodYellow;
                        break;
                }
            }
        }

        public bool PickedUp = false;
        City City;

        float opacity;

        public Pickup(Game1 game, City city)
            : base(game)
        {
            Width = 32;
            Height = 32;

            City = city;

            opacity = 0;
        }

        List<Vector2> Route = new List<Vector2>();
        int np = 0;
        public void Spawn(Vector2 velocity)
        {
            PickedUp = false;
            Position = City.Position;

            opacity = 0;
            RegisteredCollider = false;

            //Velocity = velocity;
            bool pls = false;
            
            if (velocity.X > 0)
                pls = false;
            else
                pls = true;

            float y = Position.Y;

            if (pls)
            {
                for (float x = Position.X; x > 0; x -= halfHeight)
                {
                    y = MathHelper.Hermite(y - halfHeight, 1, y + halfHeight, -1, (float)Game1.rnd.NextDouble());
                    Route.Add(Vector2.Clamp(new Vector2(x, y), minPosition, maxPosition));
                }
            }
            else
                for (float x = Position.X; x < Game1.RenderArea.Width; x += halfHeight)
                {
                    y = MathHelper.Hermite(y - halfHeight, 1, y + halfHeight, -1, (float)Game1.rnd.NextDouble());
                    Route.Add(Vector2.Clamp(new Vector2(x, y), minPosition, maxPosition));
                }

            np = 0;

            Game.PickupsInPlay.Add(this);
        }

        public override void Update(GameTime gameTime)
        {
            if (PickedUp)
                return;

            if (opacity != 1)
                opacity += .01f;

            if (opacity > 1)
                opacity = 1;


            if(opacity == 1 && !RegisteredCollider && !PickedUp)
                RegisteredCollider = true;

            Color = new Color(Color.R, Color.G, Color.B, (byte)(255 * opacity));

            if (Bounds.Intersects(new Rectangle((int)Route[np].X, (int)Route[np].Y, 4, 4)))
                np++;

            if (np >= Route.Count)
                np = Route.Count - 1;

            Velocity = Vector2.Normalize(Route[np] - Position);            

            base.Update(gameTime);

            Position = Vector2.Clamp(Position, minPosition, maxPosition);

            if (Position.X == maxPosition.X || Position.X == minPosition.X)
                KillPickup(new CollisionData(this, this));
        }
        public void KillPickup(CollisionData collisionData)
        {
            base.OnCollided(collisionData);
         
            // Take me out of the collision engine     
            
            collisionManager.UnRegister(this, false);

            if (OnPickupDied != null)
                OnPickupDied(this);

            Game.pickupstoDie.Add(this);
        }

        public override void Collided(CollisionData collisionData)
        {
            Base2DObject collidedWith = null;
            if (collisionData.Objects[0] == this)
                collidedWith = (Base2DObject)collisionData.Objects[1];
            else
                collidedWith = (Base2DObject)collisionData.Objects[0];

            if ((collidedWith is City && ((City)collidedWith).Player != City.Player) || collidedWith is Ball || collidedWith is Bat || collidedWith is Bullet)
            {
                // Pop noise
                KillPickup(collisionData);
            }

            if(PickedUp)
                base.Collided(collisionData);
        }
    }
}
